package data;

import entity.Userinfo;
import org.apache.log4j.Logger;

import java.util.ArrayList;

public class UserInfoList {
    private static ArrayList<Userinfo> userList = new ArrayList<>();
    private static Logger logger = Logger.getLogger(UserInfoList.class);

    public static void addUser(Userinfo userinfo) {
        for (Userinfo user : userList) {
            if (user.getId() == userinfo.getId())
                return;
        }
        userList.add(userinfo);
    }

    public static void outUserInfo() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("当前在线用户:");
        for (Userinfo userinfo : userList) {
            stringBuilder.append(System.getProperty("line.separator"));
            stringBuilder.append(userinfo.getId());
            stringBuilder.append(" ");
            stringBuilder.append(userinfo.getUsername());
            stringBuilder.append(" ");
            stringBuilder.append(userinfo.getOperator());
        }
        logger.info(stringBuilder.toString());
    }

    public static boolean removeUser(int id) {
        for (Userinfo userinfo : userList) {
            if (userinfo.getId() == id) {
                userList.remove(userinfo);
                return true;
            }
        }
        return false;
    }

    public static void outlogoutInfo(String id, String operator) {
        logger.info("用户登出: " + id + " " + operator);
    }

    public static void outRegisterInfo(int id, String operator){
        logger.info("新用户注册: " + id + " " + operator);
    }
}
